package com.cbgolf.oa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.ScoreHistoryBean;
import com.cbgolf.oa.entity.ScoreHistoryDataBean;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.ListViewNew;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lq on 2017/9/8.
 */

public class ScoreHistoryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<ScoreHistoryBean> list = new ArrayList<>();
    private Context context;

    private Map<Integer, Boolean> state = new HashMap<>();

    public ScoreHistoryAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void clear() {
        if (null != list && list.size() > 0) {
            this.list.clear();
        }
    }

    public void addALL(List<ScoreHistoryBean> list) {
        this.list.addAll(list);
    }

    public void addData(List<ScoreHistoryBean> list) {
        this.list.addAll(list);
    }


    public void setData(List<ScoreHistoryBean> ls) {
        this.list = ls;
    }

    public void upDataUI() {
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder h = null;
        boolean isLoad = false;
        if (null == view) {
            h = new Holder();
            isLoad = true;
            view = mInflater.inflate(R.layout.item_score_history, null);
            h.dateBarTv = (TextView) view.findViewById(R.id.item_score_date_tv);
            h.listView = (ListViewNew) view.findViewById(R.id.item_score_history_listview);
            view.setTag(h);
        } else {
            h = (Holder) view.getTag();
        }
        AutoUtil.auto(view);
        ScoreHistoryBean m = list.get(position);
        if (null == m) {
            return view;
        }
        String date = m.dateTime;
        h.dateBarTv.setText(Util.isNull(date) ? "" : date);
        List<ScoreHistoryDataBean> listData = m.datas;

        if (null != listData && listData.size() > 0) {
            if (!(state.containsKey(position) && null != state.get(position) && state.get(position))
                    || isLoad) {
                ScoreSonAdapter adapter = new ScoreSonAdapter(R.layout.item_score_history_son, context, listData);
                h.listView.setAdapter(adapter);
                state.put(position, true);
            }
        }
        return view;
    }

    class Holder {
        private TextView dateBarTv;
        private ListViewNew listView;
    }


}
